﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SpeakupBackend;
using WebLayer;
using System.ComponentModel;
using System.Collections.Specialized;
using SpeakupBackend.Classes;

namespace Frontend
{
    /// <summary>
    /// Interaction logic for TodoControl.xaml
    /// </summary>
    public partial class TodoControl : UserControl
    {
        DelegateProxy<EventHandler<EventArgs>> SubmitDoneProxy =
                new DelegateProxy<EventHandler<EventArgs>>();

        DelegateProxy<PropertyChangedEventHandler> PropertyChangedProxy =
                new DelegateProxy<PropertyChangedEventHandler>();

        DelegateProxy<NotifyCollectionChangedEventHandler> CollectionChangedProxy =
            new DelegateProxy<NotifyCollectionChangedEventHandler>();

        public TodoControl()
        {
            InitializeComponent();

            SubmitDoneProxy.ProxySource += Action_SubmtiCompleted;
            CollectionChangedProxy.ProxySource += Classes_CollectionChanged;
            PropertyChangedProxy.ProxySource += Classes_PropertyChanged;

            ActionGrid.SuperviserDone += SubmitDoneProxy.ProxyConsumer;
        }

        BackendContext m_backendCtx;
        public BackendContext BackendCtx
        {
            get
            {
                return m_backendCtx;
            }
            set
            {
                if (m_backendCtx != value)
                {
                    //unsubscribe from events from old backend ctx
                    if (m_backendCtx != null)
                    {
                        m_backendCtx.Classes.CollectionChanged -= CollectionChangedProxy.ProxyConsumer;
                        m_backendCtx.Classes.PropertyChanged -= PropertyChangedProxy.ProxyConsumer;
                    }

                    m_backendCtx = value;

                    //subscribe for class events
                    m_backendCtx.Classes.CollectionChanged += CollectionChangedProxy.ProxyConsumer;
                    m_backendCtx.Classes.PropertyChanged += PropertyChangedProxy.ProxyConsumer;


                    TodoGrid.BackendCtx = value;
                    UpdateTodoGroup();
                }
            }
        }

        void Classes_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            UpdateTodoGroup();            
        }

        void Classes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UpdateTodoGroup();
        }

        void Action_SubmtiCompleted(object sender, EventArgs e)
        {
            SubmitCompleted();
        }        

        void Submit_Click(object sender, RoutedEventArgs e)
        {
            LoginToken token = MainWindow.LoginToken;
            if (token == null)
            {
                MessageBox.Show("Please fill in login form in order to login to server.",
                    "Error: no login information",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
            else
            {
                SubmitStarted();
                ActionGrid.ApplyActions(BackendCtx.Classes, token);
            }            
        }

        private void RemoveAll_Click(object sender, RoutedEventArgs e)
        {
            if (BackendCtx != null)
            {
                foreach (Class cl in BackendCtx.Classes.Where(cl => cl.IsActionPending))
                {
                    cl.DiscardAction();
                }
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            ActionGrid.CancelActions();
        }

        void UpdateTodoGroup()
        {
            int pendingCount = 0;
            if (BackendCtx != null)
            {
                pendingCount = BackendCtx.Classes.Count(cl => cl.IsActionPending);
            }

            if (pendingCount > 0)
            {
                TodoPanel_NoPendingChanges.Visibility = Visibility.Collapsed;
                TodoPanel_PendingChanges.Visibility = Visibility.Visible;
            }
            else
            {
                TodoPanel_NoPendingChanges.Visibility = Visibility.Visible;
                TodoPanel_PendingChanges.Visibility = Visibility.Collapsed;
            }
        }

        void SubmitStarted()
        {
            ResultsGroupBox.Visibility = Visibility.Visible;
            ProgressPanel.Visibility = Visibility.Visible;

            TodoGroup.Visibility = Visibility.Collapsed;
            TodoButtonsPanel.IsEnabled = false;
        }

        void SubmitCompleted()
        {
            ProgressPanel.Visibility = Visibility.Collapsed;
            
            TodoButtonsPanel.IsEnabled = true;
            TodoGroup.Visibility = Visibility.Visible;
        }

        

        
    }
}
